package com.jiangdg.usbcamera.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jiangdg.usbcamera.R;
import com.jiangdg.usbcamera.url.UrlContext;
import com.jiangdg.usbcamera.utils.Hash;
import com.jiangdg.usbcamera.utils.HttpUtils;
import com.jiangdg.usbcamera.utils.SPUtils;
import com.jiangdg.usbcamera.view.USBCameraActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.ByteArrayEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;

public class  LoginActivity extends AppCompatActivity {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);


        if( (boolean) SPUtils.get(LoginActivity.this,"hasLogin",false)){
            etUsername.setText((String) SPUtils.get(LoginActivity.this,"userName",""));
            etPassword.setText((String) SPUtils.get(LoginActivity.this,"password",""));

        }

//        startActivity(new Intent(LoginActivity.this, USBCameraActivity.class));
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String username = etUsername.getEditableText().toString();
        String password = etPassword.getEditableText().toString();
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
            Toast.makeText(LoginActivity.this,"用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userName",etUsername.getText());
            jsonObject.put("password",Hash.digest(etPassword.getText().toString()));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ByteArrayEntity entity = null;
        try {
            entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpUtils.httpPostJsonMethod(LoginActivity.this,UrlContext.login,entity,"application/json",new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("rs",response.toString());
                try {
                    boolean success  =  response.getBoolean("status");
                    if(!success){
                        Toast.makeText(LoginActivity.this,response.getString("msg"), Toast.LENGTH_SHORT).show();
                    }else {
                        SPUtils.put(LoginActivity.this,"userInfo",response.toString());
                        SPUtils.put(LoginActivity.this,"hasLogin",true);

                        finish();
                        startActivity(new Intent(LoginActivity.this, USBCameraActivity.class));
//                        Gson gson = new Gson();
//                        LoginInfo info = gson.fromJson(response.toString(),LoginInfo.class);
//                        info.getToken();
                        SPUtils.put(LoginActivity.this,"userName",etUsername.getText() );
                        SPUtils.put(LoginActivity.this,"password", etPassword.getText().toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }
        });
    }
}